import BasePanel from "./BasePanel";
import CommonConstant from "../../constant/CommonConstant";
import { LevelData } from "../Data/LevelData";
import LevelBaseControl from "../LevelCommon/LevelBaseControl";
import LanguageManager from "../../constant/LanguageManager";
import { utils } from "../../utils/CommonUtil";
import Sound from "../../constant/SoundNameConstant";

export default class CompletePanel extends BasePanel {

    /** 获得的眼睛数*/
    public GetEyeNum: number = 10;
    private eyeNumer: number;
    private noThanks: fairygui.GLabel;
    private adBtn: fairygui.GButton;
    private rewardNumText: fairygui.GLabel;
    /** 是否开始增加眼睛数的动画*/
    private isStartTweenNum: boolean = false;
    //看廣告獲得倍數
    private multiple: number = 3;

    constructor(id: number) {
        super();
        this.m_nameID = id;

        this.InitFui();
        this.mowner = this.m_fui.displayObject;
        this.mowner.alpha = 0;
        Laya.Tween.to(this.mowner, { alpha: 1 }, 500);
        this.InitData();
    }


    private InitFui() {
        this.m_fui = window.gameFacade.ResourceMng.
            getMainCom(CommonConstant._fuiCompletePanelPath.Package, "CompletePanel", "CompletePanel");

        this.eyeNumer = this.GetEyeNum;
        let curLevelData: LevelData = window.gameFacade.LevelMng.GetCurLevelData();
        let curLevelControl: LevelBaseControl = window.gameFacade.LevelMng.GetCurLevelControl();

        let configMng = window.gameFacade.ConfigMng;
        let levelText = this.m_fui.getChild("levelText").asLabel;
        levelText.text = configMng.GetLanguageByKey(LanguageManager.PART) + "-" + curLevelData.levelNum;
        let congratulateText = this.m_fui.getChild("congratulate").asLabel
        congratulateText.text = configMng.GetLanguageByKey(LanguageManager.CONGRATULATIONS);
        let timeText = this.m_fui.getChild("timeText").asLabel;
        // timeText.text = configMng.GetLanguageByKey(LanguageManager.TIME);
        /** 本关用的时间*/
        let consumeTime = Laya.timer.currTimer - curLevelControl.startTime;
        console.log("consumeTime:"+consumeTime);
        /** 更新最快时间*/
        curLevelData.UpdateMostSpeed(consumeTime);
        timeText.text = configMng.GetLanguageByKey(LanguageManager.TIME) + ":" + utils.CommonUtils.GetMillionSecondStr(consumeTime / 1000);
        let memorialText = this.m_fui.getChild("memorialText").asLabel;
        memorialText.text = configMng.GetLanguageByKey(LanguageManager.MEMORIAL)+":";

        let memorialImages = new Array<fairygui.GLoader>();

        let curGetMemorial: number = curLevelControl.curMemorialNum;
        memorialImages.push(this.m_fui.getChild("memorialImage1").asLoader);
        memorialImages.push(this.m_fui.getChild("memorialImage2").asLoader);
        memorialImages.push(this.m_fui.getChild("memorialImage3").asLoader);

        for (let i = 0; i < curGetMemorial; i++) {
            window.gameFacade.AtlasMng.SetSpriteByName(memorialImages[i], "GameUIPanel", "memorialGetInGame");
        }
        let rewardText = this.m_fui.getChild("rewardText").asLabel;
        rewardText.text = configMng.GetLanguageByKey(LanguageManager.REWARD);
        this.rewardNumText = this.m_fui.getChild("rewardCount").asLabel;
        this.rewardNumText.text = "+" + this.GetEyeNum;
        this.adBtn = this.m_fui.getChild("adButton").asButton;
        this.adBtn.onClick(this, this.OnClickAdBtn)
        let getText = this.m_fui.getChild("get").asLabel;
        getText.text = configMng.GetLanguageByKey(LanguageManager.GET) + "x" + this.multiple;

        this.noThanks = this.m_fui.getChild("noThanks").asLabel;
        this.noThanks.text = configMng.GetLanguageByKey(LanguageManager.NO_THANKS);
        this.noThanks.visible = true;
        this.noThanks.alpha = 0;
        this.noThanks.onClick(this, this.OnClickAdBtn);
        Laya.timer.once(2000, this, this.StartTween);
        super.Show();
    }

    private InitData(){
        let curLevelData: LevelData = window.gameFacade.LevelMng.GetCurLevelData();
        //LevelManager.Instance.PlayInNextLevel();
        /** 如果已经过关了奖励减少*/
        if (curLevelData.IsHavePass)
        {
            this.GetEyeNum = 3;
        }
        else
        {
            /** 设置过关了*/
            curLevelData.IsHavePass = true;
            // TalkingDataManager.Instance.TkEvent(TalkingDataManager.PASS_LEVEL,curLevelData.levelNum.ToString());
        }
        window.gameFacade.SoundMng.playSound(Sound.soundClipArr.FINISH);
    }

    private StartTween() {
        if(this.noThanks == null){
            return;
        }
        
        Laya.Tween.to(this.noThanks, { alpha: 1 }, 500, null, Laya.Handler.create(this, function () {

        }));
    }

    private OnClickAdBtn() {
        window.gameFacade.SceneMng.EnterMainScene();
    }
}